package com.jwsoft.manager.core.integration.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bifang.engine.assistant.core.annotation.ApiService;
import com.bifang.engine.assistant.core.annotation.OpApi;
import com.bifang.engine.assistant.core.annotation.SaveOplog;
import com.bifang.engine.assistant.core.enums.AuTypeEnum;
import com.bifang.engine.assistant.core.enums.BoolEnum;
import com.bifang.engine.assistant.core.enums.FuncTypeEnum;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.data.core.util.PagerUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jwsoft.manager.common.enums.RelationTypeEnum;
import com.jwsoft.manager.common.vo.eduStudentEnrollFamily.EduStudentEnrollFamilyKeyVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollFamily.EduStudentEnrollFamilyQueryVO;
import com.jwsoft.manager.common.vo.eduStudentEnrollFamily.EduStudentEnrollFamilyVO;
import com.jwsoft.manager.core.dao.model.EduStudentEnrollFamily;
import com.jwsoft.manager.core.dao.model.EduStudentEnrollPre;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollFamilyService;
import com.jwsoft.manager.core.dao.service.EduStudentEnrollPreService;
import com.jwsoft.manager.core.integration.EduStudentEnrollFamilyIntegration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 预采集家庭关系表业务实现类
 *
 * @author wangtao
 * @since 2022-11-25
 */
@ApiService(funcCode = "edustudentenrollfamily", title = "预采集家庭关系表")
public class EduStudentEnrollFamilyIntegrationImpl implements EduStudentEnrollFamilyIntegration {

    @Autowired
    public EduStudentEnrollFamilyService eduStudentEnrollFamilyService;

    @Autowired
    public EduStudentEnrollPreService eduStudentEnrollPreService;

    @Override
    @OpApi(funcCode = "edustudentenrollfamily0001", title = "预采集家庭关系表分页查询", funcType = FuncTypeEnum.query)
    public PageInfo<EduStudentEnrollFamilyVO> getList(EduStudentEnrollFamilyQueryVO vo) {
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(1000);
        }
        PageHelper.startPage(vo.getPageNum(), vo.getPageSize());
        List<EduStudentEnrollFamily> list = eduStudentEnrollFamilyService.list();
        if (CollectionUtils.isEmpty(list)) {
            // 判断是否为空，为空则返回空参数
            return PagerUtil.createEmptyPagerInfo(vo.getPageNum(), vo.getPageSize());
        }
        // 不为空则获取分页结果
        PageInfo<EduStudentEnrollFamily> pageInfo = new PageInfo<>(list);
        // 返回参数需要通过common层的vo实体类，用该方法可以批量转换
        List<EduStudentEnrollFamilyVO> resultList = Convert.toList(EduStudentEnrollFamilyVO.class, pageInfo.getList());
        // 转义数据字典值
        // dictHelper.valueToName(resultList, Arrays.asList(DemoDictEnum.values()));
        return PagerUtil.parsePagerVo(resultList, pageInfo);
    }

    @Override
    @OpApi(funcCode = "edustudentenrollfamily0002", title = "预采集家庭关系表根据主键查询详情", funcType = FuncTypeEnum.query)
    public EduStudentEnrollFamilyVO getById(EduStudentEnrollFamilyKeyVO vo) {
        EduStudentEnrollFamily entity = eduStudentEnrollFamilyService.getById(vo.getStudentPreFamilyId());
        if (ObjectUtils.isEmpty(entity)) {
            throw new AppException("查询不到详细信息!");
        }
        EduStudentEnrollFamilyVO result = Convert.convert(EduStudentEnrollFamilyVO.class, entity);
        return result;
    }

    /**
     * 保存前统一校验
     */
    @Override
    @OpApi(funcCode = "edustudentenrollfamily0003", title = "预采集家庭关系表保存前校验", funcType = FuncTypeEnum.other, checkPrivilege = BoolEnum.FALSE)
    public void checkSave(EduStudentEnrollFamilyVO vo) {
        //主键为空为新增校验

        //主键不为空为修改校验
    }

    @Override
    @OpApi(funcCode = "edustudentenrollfamily0004", title = "预采集家庭关系表新增", funcType = FuncTypeEnum.insert)
    @SaveOplog(
            operationType = "edustudentenrollfamily0004",
            operationName = "预采集家庭关系表新增",
            dataType = "studentPreFamilyId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentEnrollFamilyVO add(EduStudentEnrollFamilyVO vo) {
        //保存前校验
        checkSave(vo);
        //保存数据
        EduStudentEnrollFamily entity = Convert.convert(EduStudentEnrollFamily.class, vo);
        eduStudentEnrollFamilyService.save(entity);
        //将主键设置回vo使操作日志可以取到
        vo.setStudentPreFamilyId(entity.getStudentPreFamilyId());
        return vo;
    }

    @Override
    @OpApi(funcCode = "edustudentenrollfamily0005", title = "预采集家庭关系表修改", funcType = FuncTypeEnum.update)
    @SaveOplog(
            operationType = "edustudentenrollfamily0005",
            operationName = "预采集家庭关系表修改",
            dataType = "studentPreFamilyId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public EduStudentEnrollFamilyVO edit(EduStudentEnrollFamilyVO vo) {
        if (ObjectUtils.isEmpty(vo.getStudentPreFamilyId())) {
            throw new AppException("传入需要修改的主键不能为空!");
        }
        EduStudentEnrollFamily oldEntity = eduStudentEnrollFamilyService.getById(vo.getStudentPreFamilyId());
        if (ObjectUtils.isEmpty(oldEntity)) {
            throw new AppException("查询不到需要修改的信息!");
        }
        //保存前校验
        checkSave(vo);
        //保存数据
        EduStudentEnrollFamily entity = Convert.convert(EduStudentEnrollFamily.class, vo);
        eduStudentEnrollFamilyService.updateById(entity);
        return vo;
    }

    @Override
    @OpApi(funcCode = "edustudentenrollfamily0006", title = "预采集家庭关系表根据主键删除", funcType = FuncTypeEnum.delete)
    @SaveOplog(
            operationType = "edustudentenrollfamily0006",
            operationName = "预采集家庭关系表删除",
            dataType = "studentPreFamilyId",
            auType = AuTypeEnum.OTHER)
    @Transactional
    public void del(EduStudentEnrollFamilyKeyVO vo) {

        //修改预采集查询数据时删除监护人信息
        String idcard = vo.getRelationshipIdcard();
        String relationshipType = vo.getRelationshipType();
        if (!ObjectUtils.isEmpty(idcard)) {
            QueryWrapper<EduStudentEnrollFamily> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().
                    eq(
                            EduStudentEnrollFamily::getIdcard,
                            idcard).
                    eq(
                            !ObjectUtils.isEmpty(relationshipType),
                            EduStudentEnrollFamily::getRelationshipType,
                            relationshipType);
            List<EduStudentEnrollFamily> list = eduStudentEnrollFamilyService.list(queryWrapper);
            for (EduStudentEnrollFamily family : list) {
                String studentEnrollPreId = family.getStudentEnrollPreId();
                EduStudentEnrollPre eduStudentEnrollPre = eduStudentEnrollPreService.getById(studentEnrollPreId);
                if (RelationTypeEnum.father.getType().equals(relationshipType)){
                    eduStudentEnrollPre.setFatherId(null);
                    eduStudentEnrollPre.setFatherName("");
                    eduStudentEnrollPre.setFatherCardType("");
                    eduStudentEnrollPre.setFatherCellphone("");
                    eduStudentEnrollPre.setFatherWork("");
                    eduStudentEnrollPre.setFatherIdcard("");
                } else if (RelationTypeEnum.mother.getType().equals(relationshipType)){
                    eduStudentEnrollPre.setMotherId(null);
                    eduStudentEnrollPre.setMotherName("");
                    eduStudentEnrollPre.setMotherCardType("");
                    eduStudentEnrollPre.setMotherCellphone("");
                    eduStudentEnrollPre.setMotherWork("");
                    eduStudentEnrollPre.setMotherIdcard("");
                }else {
                    if (eduStudentEnrollPre.getFatherIdcard().equals(idcard)){
                        eduStudentEnrollPre.setFatherId(null);
                        eduStudentEnrollPre.setFatherName("");
                        eduStudentEnrollPre.setFatherCardType("");
                        eduStudentEnrollPre.setFatherCellphone("");
                        eduStudentEnrollPre.setFatherWork("");
                        eduStudentEnrollPre.setFatherIdcard("");
                    }
                }
                eduStudentEnrollPreService.updateById(eduStudentEnrollPre);
            }
            eduStudentEnrollFamilyService.remove(queryWrapper);
        } else {
            if (ObjectUtils.isEmpty(vo.getStudentPreFamilyId())) {
                throw new AppException("传入需要删除的数据主键不能为空!");
            }
            EduStudentEnrollFamily entity = eduStudentEnrollFamilyService.getById(vo.getStudentPreFamilyId());
            if (ObjectUtils.isEmpty(entity)) {
                throw new AppException("未找到需要删除的信息!");
            }
            eduStudentEnrollFamilyService.removeById(vo.getStudentPreFamilyId());
        }
    }
}
